<template>
  <div class="cart-list-item">
    <div class="checked">
      <check-button :is-checked="goods.isChecked" @click.native="checkBtn" />
    </div>
    <!-- 图片 -->
    <div class="item-img">
      <img :src="goods.image" @load="imgLoad" />
    </div>
    <!-- 内容 -->
    <div class="content">
      <div class="item-title">{{ goods.title }}</div>
      <div class="item-desc">{{ goods.desc }}</div>
      <div class="item-bottom">
        <div class="item-price bottom-left">￥{{ goods.price }}</div>
        <div class="item-count bottom-right">
          <div class="btn" @click="decrement">-</div>
          <span class="count">{{ goods.count }}</span>
          <div class="btn" @click="increment">+</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import CheckButton from 'components/common/checkButton/CheckButton'

export default {
  name: 'CartListItem',
  components: { CheckButton },
  props: {
    goods: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  methods: {
    imgLoad() {
      this.$bus.$emit('imgLoad')
    },
    checkBtn() {
      // this.$store.dispatch()
      this.goods.isChecked = !this.goods.isChecked
    },
    // 通知Vuex增加1
    increment() {
      this.$store.dispatch('addCart', this.goods)
    },
    // 通知Vuex减少1
    decrement() {
      if (this.goods.count !== 1) {
        this.$store.dispatch('deCount', this.goods)
      }
    }
  }
}
</script>

<style type="text/css" scoped>
.cart-list-item {
  display: flex;
  padding: 5px;
  border-bottom: 1px solid #ccc;
}
.checked {
  width: 25px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-content: center;
}
.content {
  font-size: 17px;
  color: #333;
  padding: 5px 10px;
  flex: 1;
  width: 10%;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}
.item-bottom {
  display: flex;
  justify-content: space-between;
}
.item-title,
.item-desc {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.item-img {
  padding: 5px;
  /*border: 1px solid #ccc;*/
}

.item-img img {
  width: 80px;
  height: 100px;
  display: block;
  border-radius: 5px;
}

.item-desc {
  font-size: 14px;
  color: #666;
}

.item-price {
  color: orangered;
}
.count {
  display: inline-block;
  text-align: center;
  width: 30px;
  height: 20px;
  line-height: 20px;
  font-size: 14px;
  border: 1px solid #ccc;
}
.btn {
  display: inline-block;
  width: 18px;
  height: 20px;
  border: 1px solid #ccc;
  background: #e0e0e0;
  text-align: center;
  line-height: 20px;
}
</style>
